package cz.muni.fi.pa165.pokemon.web;

import cz.muni.fi.pa165.pokemon.dto.BadgeTO;
import cz.muni.fi.pa165.pokemon.service.BadgeService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Michal Toth
 */
@UrlBinding("/badge/{$event}/")
public class BadgeActionBean implements ActionBean {

    private final static Logger log = LoggerFactory.getLogger(BadgeActionBean.class);
    private ActionBeanContext context;
    private List<BadgeTO> foundList;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "firstName", required = true),
        @Validate(on = {"add", "save"}, field = "lastName", required = true),
        @Validate(on = {"add", "save"}, field = "address", required = true)
    })
    private BadgeTO cto;

    @SpringBean
    protected BadgeService badgeService;

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/badge/list.jsp");
    }

    public List<BadgeTO> getBadges() {
        return badgeService.findAll();
    }

    @Override
    public void setContext(ActionBeanContext abc) {
        this.context = abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public BadgeTO getCto() {
        return cto;
    }

    public void setCto(BadgeTO cto) {
        this.cto = cto;
    }

    public Resolution add() {
        log.debug("newbadge() cto={}", cto);
        badgeService.create(cto);
        return new RedirectResolution(this.getClass(), "all");
        
    }
    
    public Resolution create() {
        log.debug("create()");
        return new ForwardResolution("/badge/create.jsp");
    }

    public Resolution delete() {
        Long id = Long.parseLong(context.getRequest().getParameter("cto.id"));
        cto = badgeService.get(id);
        log.debug("deleteBadge() cto={}", cto);
        badgeService.remove(cto);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadBadgeFromDatabase() {
        String ids = context.getRequest().getParameter("cto.id");
        if (ids == null) {
            return;
        }
        cto = badgeService.get(Long.parseLong(ids));
    }

    public Resolution edit() {
        log.debug("edit() cto={}", cto);
        return new ForwardResolution("/badge/edit.jsp");
    }

    public Resolution save() {
        log.debug("save() cto={}", cto);
        badgeService.update(cto);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution findByName() {
        log.debug("findByName() ");
        //HttpServletRequest req = context.getRequest();
        //System.out.println(req.getContextPath() + "./." + req.getServletPath() + "./." + req.getPathInfo() + "\n" + req.getParameterMap());
        String fn = context.getRequest().getParameter("firstname");
        String ln = context.getRequest().getParameter("lastname");

        if (!fn.equals("") && !ln.equals("")) {
            foundList = badgeService.findByName(fn, ln);
        } else if (ln.equals("")) {
            foundList = badgeService.findByName(fn, null);
        } else if (fn.equals("")) {
            foundList = badgeService.findByName(null, ln);
        } else {
            return new RedirectResolution(this.getClass(), "all");
        }
        //log.debug(foundList.toString());
        return new ForwardResolution("/badge/resultlist.jsp");
    }

    /**
     * returns list of found badges
     */
    public List<BadgeTO> getFoundList() {
        return foundList;
    }
}


//    public void fillDB() {
//        BadgeTO cto1 = new BadgeTO();
//        BadgeTO cto2 = new BadgeTO();
//        BadgeTO cto3 = new BadgeTO();
//        cto1.setFirstName("Lukas");
//        cto1.setFirstName("Novy");
//        cto1.setAddress("Polna 22, Brno");
//
//        cto2.setFirstName("Lukas");
//        cto2.setFirstName("Novotny");
//        cto2.setAddress("Prazska 2, Praha");
//
//        cto3.setFirstName("Tomas");
//        cto3.setFirstName("Novy");
//        cto3.setAddress("Domazlicka 36, Bratislava");
//
//        badgeService.create(cto1);
//        badgeService.create(cto2);
//        badgeService.create(cto3);
//    }